﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Bundled.Entity
{
    public sealed class ShoppingCart
    {
        private static ShoppingCart instance;

        public struct Item
        {
            public int ProductID { get; set; }
            public int Quantity { get; set; }
            public decimal Price { get; set; }
            public string PartName { get; set; }
        }

        private List<Item> Items { get; set; }
        public decimal Total { get; set; }

        private ShoppingCart() { }

        public static ShoppingCart GetCart()
        {
            if (instance == null)
            {
                instance = new ShoppingCart();
                instance.Items = new List<Item>();
                instance.Total = 0;
            }
            return instance;
        }

        public static void AddToCart(int productID, int quantity, decimal price, string partName)
        {
            if (instance != null)
            {
                int index = instance.Items.FindIndex(i => i.ProductID == productID);

                if (index != -1)
                {
                    Item item = instance.Items[index];
                    item.Quantity += quantity;
                    instance.Items[index] = item;
                }
                else
                {
                    instance.Items.Add(new Item() { ProductID = productID, Quantity = quantity, Price = price, PartName = partName });
                }
                GetTotal();
            }
        }

        public static void UpdateCart(int productID, int quantity)
        {
            if (instance != null)
            {
                int index = instance.Items.FindIndex(i => i.ProductID == productID);
                if (index != -1)
                {
                    Item item = instance.Items[index];
                    if (item.Quantity != quantity)
                    {
                        item.Quantity = quantity;
                    }
                    instance.Items[index] = item;
                }
                GetTotal();
            }
        }

        public static void RemoveFromCart(int productID)
        {
            if (instance != null)
            {
                int index = instance.Items.FindIndex(i => i.ProductID == productID);
                if (index != -1)
                {
                    Item item = instance.Items[index];
                    instance.Items.RemoveAt(index);
                }
                GetTotal();
            }
        }

        private static void GetTotal()
        {
            decimal total = 0;
            foreach (Item item in instance.Items)
            {
                total += (item.Price * item.Quantity);
            }
            instance.Total = total;
        }

        public List<Item> ItemList
        {
            get { return instance.Items; }
        }
    }
}